package com.finiac.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.finiac.model.Product;

public class ProductDAOImpl implements ProductDAO {

	private HibernateTemplate hibernateTemplate;
	
	public void setSessionFactory(SessionFactory sessionFactory){
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	}
	
	@Override
	public void addProduct(Product product) {
		hibernateTemplate.saveOrUpdate(product);

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Product> listProduct() {
		return hibernateTemplate.find("from Product where isActive = 1 order by id");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Product> selectById(int id) {
		return hibernateTemplate.find("from Product where id="+id + " and isActive = 1");
	}

	@Override
	public void updateProduct(Product product) {
		hibernateTemplate.update(product);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Product> autoCom(String text)
	{
		return hibernateTemplate.find("from Product where product like ? and isActive = 1", "%" + text + "%");
	}
	
}
